package com.dmap.community.service.community.impl;

import com.dmap.community.bo.reply.ReplyBo;
import com.dmap.community.bo.reply.ReplyCommentBo;
import com.dmap.community.cache.UserCacheDao;
import com.dmap.community.dao.ReplyCommentMapper;
import com.dmap.community.dao.ReplyMapper;
import com.dmap.community.po.Reply;
import com.dmap.community.po.ReplyComment;
import com.dmap.community.po.ReplyCommentExample;
import com.dmap.community.po.ReplyExample;
import com.dmap.community.po.cache.UserCachePo;
import com.dmap.community.service.community.PraiseService;
import com.dmap.community.service.community.ReplyService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * \* User: silent
 * \* Date: 14/11/17 Time: 10:52
 * \* Description:
 * \
 */
@Service
public class ReplyServiceImpl implements ReplyService {

    @Resource
    private ReplyMapper replyMapper;
    @Resource
    private ReplyCommentMapper replyCommentMapper;

    @Resource
    private PraiseService praiseService;

    @Resource
    private UserCacheDao userCacheDao;

    @Override
    public int insertReplyTheme(long themeId, long userId, String comment) {
        Reply reply = new Reply();
        reply.setCreateTime(System.currentTimeMillis() / 1000);
        reply.setThemeId(themeId);
        reply.setReplyComment(comment);
        reply.setUserId(userId);
        return replyMapper.insertSelective(reply);
    }

    @Override
    public int insetReplyComment(long replyId, long userId, String comment) {
        ReplyComment replyComment = new ReplyComment();
        replyComment.setReplyId(replyId);
        replyComment.setReplyContent(comment);
        replyComment.setUserId(userId);
        replyComment.setCreateTime(System.currentTimeMillis() / 1000);
        return replyCommentMapper.insertSelective(replyComment);
    }

    /**
     * 删除主题品论
     * first step 删除主题评论的点赞信息
     * second step  删除评论的的回复和点赞信息
     */
    @Transactional
    @Override
    public int deleteReplyTheme(long themeId) {
        ReplyExample replyExample = new ReplyExample();
        replyExample.createCriteria().andThemeIdEqualTo(themeId);
        List<Reply> replyList = replyMapper.selectByExample(replyExample);
        replyList.forEach(reply -> {
            praiseService.deleteReplyPraise(reply.getId(),reply.getUserId());
            deleteReplyComment(reply.getId());
        });
        return replyMapper.deleteByExample(replyExample);
    }

    @Transactional
    @Override
    public int deleteReplyComment(long replyId) {
        ReplyCommentExample replyCommentExample = new ReplyCommentExample();
        replyCommentExample.createCriteria().andReplyIdEqualTo(replyId);
        List<ReplyComment> replyCommentList = replyCommentMapper.selectByExample(replyCommentExample);
        replyCommentList.forEach(replyComment -> {
            praiseService.deleteReplyCommentPraise(replyComment.getId(),replyComment.getUserId());
        });
        return replyCommentMapper.deleteByExample(replyCommentExample);
    }

    @Override
    public int countReplyThemeNumber(long themeId) {
        ReplyExample replyExample = new ReplyExample();
        replyExample.createCriteria().andThemeIdEqualTo(themeId);
        return replyMapper.countByExample(replyExample);
    }

    @Override
    public List<ReplyBo> selectReplyTheme(long themeId, int pageIndex, int pageSize) {
        List<ReplyBo> replyBoList = new ArrayList<>(10);
        ReplyExample replyExample = new ReplyExample();
        replyExample.setOrderByClause("asc create_time");
        replyExample.setLimitStart((pageIndex - 1) * pageSize);
        replyExample.setLimitEnd(pageSize);
        replyExample.createCriteria().andThemeIdEqualTo(themeId);
        List<Reply> replyList = replyMapper.selectByExample(replyExample);
        replyList.forEach(reply -> {
            ReplyBo replyBo = new ReplyBo(reply);
            UserCachePo userCachePo = userCacheDao.selectUserCacheInformation(reply.getUserId());
            replyBo.setNikName(userCachePo.getNickname());
            replyBo.setPortraitUrl(userCachePo.getPortraitUrl());
            replyBo.setPraiseNumber(praiseService.countReplyPraise(reply.getId()));
            replyBoList.add(replyBo);
        });
        return replyBoList;
    }

    @Override
    public List<ReplyCommentBo> selectReplyComment(long replyId, int pageIndex, int pageSize) {
        List<ReplyCommentBo> replyBoList = new ArrayList<>(10);
        ReplyCommentExample replyCommentExample = new ReplyCommentExample();
        replyCommentExample.setOrderByClause("asc create_time");
        replyCommentExample.setLimitStart((pageIndex - 1) * pageSize);
        replyCommentExample.setLimitEnd(pageSize);
        List<ReplyComment> replyCommentList = replyCommentMapper.selectByExample(replyCommentExample);
        replyCommentList.forEach(replyComment -> {
            ReplyCommentBo replyCommentBo = new ReplyCommentBo(replyComment);
            UserCachePo userCachePo = userCacheDao.selectUserCacheInformation(replyCommentBo.getUserId());
            replyCommentBo.setNikName(userCachePo.getNickname());
            replyCommentBo.setPortraitUrl(userCachePo.getPortraitUrl());
            replyCommentBo.setPraiseNumber(praiseService.countReplyCommentPraise(replyComment.getId()));
            replyBoList.add(replyCommentBo);
        });
        return replyBoList;
    }

}
